/*     */ package com.zimbra.cs.service.formatter;
/*     */ 
/*     */ import com.zimbra.common.account.Key.CosBy;
/*     */ import com.zimbra.common.httpclient.HttpClientUtil;
/*     */ import com.zimbra.common.mailbox.Color;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.HttpUtil;
/*     */ import com.zimbra.common.util.Pair;
/*     */ import com.zimbra.common.util.WebSplitUtil;
/*     */ import com.zimbra.common.util.ZimbraHttpConnectionManager;
/*     */ import com.zimbra.cs.account.Account;
/*     */ import com.zimbra.cs.account.AuthToken;
/*     */ import com.zimbra.cs.account.AuthTokenException;
/*     */ import com.zimbra.cs.account.Cos;
/*     */ import com.zimbra.cs.account.GuestAccount;
/*     */ import com.zimbra.cs.account.Provisioning;
/*     */ import com.zimbra.cs.account.Server;
/*     */ import com.zimbra.cs.mailbox.Folder;
/*     */ import com.zimbra.cs.mailbox.MailItem;
/*     */ import com.zimbra.cs.mailbox.MailItem.Type;
/*     */ import com.zimbra.cs.mailbox.Mountpoint;
/*     */ import com.zimbra.cs.service.AuthProvider;
/*     */ import com.zimbra.cs.service.UserServlet;
/*     */ import com.zimbra.cs.service.UserServlet.HttpInputStream;
/*     */ import com.zimbra.cs.service.UserServletContext;
/*     */ import com.zimbra.cs.service.UserServletContext.FakeFolder;
/*     */ import com.zimbra.cs.service.UserServletException;
/*     */ import java.io.IOException;
/*     */ import java.util.Enumeration;
/*     */ import javax.servlet.RequestDispatcher;
/*     */ import javax.servlet.Servlet;
/*     */ import javax.servlet.ServletConfig;
/*     */ import javax.servlet.ServletContext;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import org.apache.commons.httpclient.Header;
/*     */ import org.apache.commons.httpclient.HttpClient;
/*     */ import org.apache.commons.httpclient.methods.GetMethod;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public class HtmlFormatter
/*     */   extends Formatter
/*     */ {
/*     */   private static final String PATH_MAIN_CONTEXT = "/zimbra";
/*     */   private static final String PATH_JSP_REST_PAGE = "/h/rest";
/*     */   private static final long AUTH_EXPIRATION = 3600000L;
/*     */   private static final String ATTR_REQUEST_URI = "zimbra_request_uri";
/*     */   private static final String ATTR_INTERNAL_DISPATCH = "zimbra_internal_dispatch";
/*     */   private static final String ATTR_AUTH_TOKEN = "zimbra_authToken";
/*     */   private static final String ATTR_CSRF_ENABLED = "zimbra_csrfEnabled";
/*     */   private static final String ATTR_TARGET_ACCOUNT_NAME = "zimbra_target_account_name";
/*     */   private static final String ATTR_TARGET_ACCOUNT_ID = "zimbra_target_account_id";
/*     */   private static final String ATTR_TARGET_ITEM_ID = "zimbra_target_item_id";
/*     */   private static final String ATTR_TARGET_ITEM_TYPE = "zimbra_target_item_type";
/*     */   private static final String ATTR_TARGET_ITEM_COLOR = "zimbra_target_item_color";
/*     */   private static final String ATTR_TARGET_ITEM_VIEW = "zimbra_target_item_view";
/*     */   private static final String ATTR_TARGET_ITEM_PATH = "zimbra_target_item_path";
/*     */   private static final String ATTR_TARGET_ITEM_NAME = "zimbra_target_item_name";
/*     */   private static final String ATTR_TARGET_ACCOUNT_PREF_TIME_ZONE = "zimbra_target_account_prefTimeZoneId";
/*     */   private static final String ATTR_TARGET_ACCOUNT_PREF_SKIN = "zimbra_target_account_prefSkin";
/*     */   private static final String ATTR_TARGET_ACCOUNT_PREF_LOCALE = "zimbra_target_account_prefLocale";
/*     */   private static final String ATTR_TARGET_ACCOUNT_PREF_CALENDAR_FIRST_DAY_OF_WEEK = "zimbra_target_account_prefCalendarFirstDayOfWeek";
/*     */   private static final String ATTR_TARGET_ACCOUNT_PREF_CALENDAR_DAY_HOUR_START = "zimbra_target_account_prefCalendarDayHourStart";
/*     */   private static final String ATTR_TARGET_ACCOUNT_PREF_CALENDAR_DAY_HOUR_END = "zimbra_target_account_prefCalendarDayHourEnd";
/*     */   
/*     */   public void formatCallback(UserServletContext context)
/*     */     throws UserServletException, ServiceException, IOException, ServletException
/*     */   {
/*  84 */     dispatchJspRest(context.getServlet(), context);
/*     */   }
/*     */   
/*     */   public FormatterFactory.FormatType getType()
/*     */   {
/*  89 */     return FormatterFactory.FormatType.HTML;
/*     */   }
/*     */   
/*     */   static void dispatchJspRest(Servlet servlet, UserServletContext context) throws ServiceException, ServletException, IOException
/*     */   {
/*  94 */     AuthToken auth = null;
/*  95 */     long expiration = System.currentTimeMillis() + 3600000L;
/*  96 */     if (context.basicAuthHappened) {
/*  97 */       Account acc = context.getAuthAccount();
/*  98 */       if ((acc instanceof GuestAccount)) {
/*  99 */         auth = AuthToken.getAuthToken(acc.getId(), acc.getName(), null, ((GuestAccount)acc).getDigest(), expiration);
/*     */       } else {
/* 101 */         auth = AuthProvider.getAuthToken(context.getAuthAccount(), expiration);
/*     */       }
/* 103 */     } else if (context.cookieAuthHappened) {
/* 104 */       auth = UserServlet.getAuthTokenFromCookie(context.req, context.resp, true);
/*     */     } else {
/* 106 */       auth = AuthToken.getAuthToken("99999999-9999-9999-9999-999999999999", null, null, null, expiration);
/*     */     }
/* 108 */     if ((auth != null) && (context.targetAccount != null) && (context.targetAccount != context.getAuthAccount())) {
/* 109 */       auth.setProxyAuthToken(Provisioning.getInstance().getProxyAuthToken(context.targetAccount.getId(), null));
/*     */     }
/* 111 */     String authString = null;
/*     */     try {
/* 113 */       if (auth != null)
/* 114 */         authString = auth.getEncoded();
/*     */     } catch (AuthTokenException e) {
/* 116 */       throw new ServletException("error generating the authToken", e);
/*     */     }
/*     */     
/* 119 */     Account targetAccount = context.targetAccount;
/* 120 */     MailItem targetItem = context.target;
/* 121 */     String uri = (String)context.req.getAttribute("requestedPath");
/*     */     
/* 123 */     if (((targetItem instanceof Mountpoint)) && (((Mountpoint)targetItem).getDefaultView() != MailItem.Type.APPOINTMENT)) {
/* 124 */       Mountpoint mp = (Mountpoint)targetItem;
/* 125 */       Provisioning prov = Provisioning.getInstance();
/* 126 */       targetAccount = prov.getAccountById(mp.getOwnerId());
/* 127 */       Pair<Header[], UserServlet.HttpInputStream> remoteItem = UserServlet.getRemoteResourceAsStream(auth == null ? null : auth.toZAuthToken(), mp.getTarget(), context.extraPath);
/* 128 */       ((UserServlet.HttpInputStream)remoteItem.getSecond()).close();
/* 129 */       String remoteItemId = null;
/* 130 */       String remoteItemType = null;
/* 131 */       String remoteItemName = null;
/* 132 */       String remoteItemPath = null;
/* 133 */       for (Header h : (Header[])remoteItem.getFirst()) {
/* 134 */         if (h.getName().compareToIgnoreCase("X-Zimbra-ItemId") == 0) {
/* 135 */           remoteItemId = h.getValue();
/* 136 */         } else if (h.getName().compareToIgnoreCase("X-Zimbra-ItemType") == 0) {
/* 137 */           remoteItemType = h.getValue();
/* 138 */         } else if (h.getName().compareToIgnoreCase("X-Zimbra-ItemName") == 0) {
/* 139 */           remoteItemName = h.getValue();
/* 140 */         } else if (h.getName().compareToIgnoreCase("X-Zimbra-ItemPath") == 0)
/* 141 */           remoteItemPath = h.getValue();
/*     */       }
/* 143 */       context.req.setAttribute("zimbra_target_item_id", remoteItemId);
/* 144 */       context.req.setAttribute("zimbra_target_item_type", remoteItemType);
/* 145 */       context.req.setAttribute("zimbra_target_item_name", remoteItemName);
/* 146 */       context.req.setAttribute("zimbra_target_item_path", remoteItemPath);
/* 147 */       context.req.setAttribute("zimbra_target_item_color", Byte.valueOf(mp.getColor()));
/* 148 */       context.req.setAttribute("zimbra_target_item_view", Byte.valueOf(mp.getDefaultView().toByte()));
/* 149 */       targetItem = null;
/*     */     }
/*     */     
/* 152 */     context.req.setAttribute("zimbra_internal_dispatch", "yes");
/* 153 */     context.req.setAttribute("zimbra_request_uri", uri != null ? uri : context.req.getRequestURI());
/* 154 */     context.req.setAttribute("zimbra_authToken", authString);
/* 155 */     context.req.setAttribute("zimbra_csrfEnabled", Boolean.valueOf(auth.isCsrfTokenEnabled()));
/* 156 */     if (targetAccount != null) {
/* 157 */       context.req.setAttribute("zimbra_target_account_name", targetAccount.getName());
/* 158 */       context.req.setAttribute("zimbra_target_account_id", targetAccount.getId());
/* 159 */       context.req.setAttribute("zimbra_target_account_prefTimeZoneId", targetAccount.getAttr("zimbraPrefTimeZoneId"));
/* 160 */       context.req.setAttribute("zimbra_target_account_prefSkin", targetAccount.getAttr("zimbraPrefSkin"));
/* 161 */       context.req.setAttribute("zimbra_target_account_prefLocale", targetAccount.getAttr("zimbraPrefLocale"));
/* 162 */       context.req.setAttribute("zimbra_target_account_prefCalendarFirstDayOfWeek", targetAccount.getAttr("zimbraPrefCalendarFirstDayOfWeek"));
/* 163 */       context.req.setAttribute("zimbra_target_account_prefCalendarDayHourStart", targetAccount.getAttr("zimbraPrefCalendarDayHourStart"));
/* 164 */       context.req.setAttribute("zimbra_target_account_prefCalendarDayHourEnd", targetAccount.getAttr("zimbraPrefCalendarDayHourEnd"));
/*     */     }
/*     */     else {
/* 167 */       if (context.fakeTarget != null) {
/* 168 */         context.req.setAttribute("zimbra_target_account_name", context.fakeTarget.getAccount());
/*     */       }
/* 170 */       Cos defaultCos = Provisioning.getInstance().get(Key.CosBy.name, "default");
/* 171 */       context.req.setAttribute("zimbra_target_account_prefTimeZoneId", defaultCos.getAttr("zimbraPrefTimeZoneId"));
/*     */       
/* 173 */       context.req.setAttribute("zimbra_target_account_prefSkin", defaultCos.getAttr("zimbraPrefSkin"));
/* 174 */       context.req.setAttribute("zimbra_target_account_prefLocale", defaultCos.getAttr("zimbraPrefLocale"));
/* 175 */       context.req.setAttribute("zimbra_target_account_prefCalendarFirstDayOfWeek", defaultCos.getAttr("zimbraPrefCalendarFirstDayOfWeek"));
/* 176 */       context.req.setAttribute("zimbra_target_account_prefCalendarDayHourStart", defaultCos.getAttr("zimbraPrefCalendarDayHourStart"));
/* 177 */       context.req.setAttribute("zimbra_target_account_prefCalendarDayHourEnd", defaultCos.getAttr("zimbraPrefCalendarDayHourEnd"));
/*     */     }
/* 179 */     if (targetItem != null) {
/* 180 */       context.req.setAttribute("zimbra_target_item_id", Integer.valueOf(targetItem.getId()));
/* 181 */       context.req.setAttribute("zimbra_target_item_path", targetItem.getPath());
/* 182 */       context.req.setAttribute("zimbra_target_item_name", targetItem.getName());
/* 183 */       context.req.setAttribute("zimbra_target_item_type", targetItem.getType().toString());
/*     */       
/* 185 */       context.req.setAttribute("zimbra_target_item_color", Byte.valueOf(targetItem.getColor()));
/* 186 */       if ((targetItem instanceof Folder)) {
/* 187 */         context.req.setAttribute("zimbra_target_item_view", ((Folder)targetItem).getDefaultView().toString());
/*     */       }
/*     */     } else {
/* 190 */       context.req.setAttribute("zimbra_target_item_color", Byte.valueOf(Color.getMappedColor(null)));
/*     */     }
/* 192 */     if (context.fakeTarget != null) {
/* 193 */       context.req.setAttribute("zimbra_target_item_path", context.fakeTarget.getPath());
/* 194 */       context.req.setAttribute("zimbra_target_item_name", context.fakeTarget.getName());
/*     */     }
/* 196 */     String mailUrl = "/zimbra";
/* 197 */     if (WebSplitUtil.isZimbraServiceSplitEnabled()) {
/* 198 */       mailUrl = Provisioning.getInstance().getLocalServer().getWebClientURL() + "/h/rest";
/* 199 */       HttpClient httpclient = ZimbraHttpConnectionManager.getInternalHttpConnMgr().getDefaultHttpClient();
/*     */       
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 218 */       Enumeration<String> attributeNames = context.req.getAttributeNames();
/* 219 */       StringBuilder sb = new StringBuilder(mailUrl);
/* 220 */       sb.append("?");
/* 221 */       while (attributeNames.hasMoreElements())
/*     */       {
/* 223 */         String attrName = (String)attributeNames.nextElement();
/* 224 */         String attrValue = context.req.getAttribute(attrName).toString();
/* 225 */         sb.append(attrName).append("=").append(HttpUtil.urlEscape(attrValue)).append("&");
/*     */       }
/* 227 */       GetMethod postMethod = new GetMethod(sb.toString());
/*     */       
/* 229 */       HttpClientUtil.executeMethod(httpclient, postMethod);
/* 230 */       ByteUtil.copy(postMethod.getResponseBodyAsStream(), true, context.resp.getOutputStream(), false);
/*     */     } else {
/*     */       try {
/* 233 */         mailUrl = Provisioning.getInstance().getLocalServer().getMailURL();
/*     */       }
/*     */       catch (Exception e) {}
/* 236 */       ServletContext targetContext = servlet.getServletConfig().getServletContext().getContext(mailUrl);
/* 237 */       RequestDispatcher dispatcher = targetContext.getRequestDispatcher("/h/rest");
/* 238 */       dispatcher.forward(context.req, context.resp);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/service/formatter/HtmlFormatter.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */